package com.homwee.factory;

//import com.changhong.tvos.common.TVManager;
//import com.changhong.tvos.common.exception.TVManagerNotInitException;
import com.homwee.factory.util.Contants;
import com.homwee.tvap.common.comSettingInterface.factoryPage1;
import com.mediatek.twoworlds.factory.MtkTvFApiDisplayTest;
import com.mediatek.twoworlds.factory.MtkTvFApiInformation;
//zcj 2018-10-30 import com.changhong.tvos.service.TvCommonManager;
//zcj 2018-10-30 import com.changhong.tvos.service.TvFactoryManager;
//zcj 2018-10-30 import com.changhong.tvos.service.TvMiscManager;

import android.content.Context;
import android.content.Intent;
import android.os.SystemProperties;
import android.provider.Settings;
import android.util.Log;
import android.view.Gravity;
//zcj 2018-10-31 import android.os.SystemProperties;
import android.widget.TextView;
import java.util.List;
import android.app.ActivityManager;

import com.mediatek.twoworlds.tv.MtkTvUtil;

import static android.provider.Settings.System.SCREEN_OFF_TIMEOUT;
import static com.homwee.factory.FactoryKeyReciver.mBrand;

//for local dimming vop pattern
import com.mediatek.twoworlds.factory.MtkTvFApiDisplayTest;
import com.mediatek.twoworlds.factory.model.MtkTvFApiDispTstPattern;


public class FactoryModeMenu extends SystemMenu {
	static FactoryModeMenu mFactoryModeMenu;
        private boolean isServiceWorked = false;

	TextView mMTextView;
	TextView mATextView;
	
	Context mContext=null;

	public static FactoryModeMenu getInstance(Context context) {
		if (mFactoryModeMenu == null) {
			mFactoryModeMenu = new FactoryModeMenu(context);
			mFactoryModeMenu.show();

		}
		return mFactoryModeMenu;
	}

	public static void refresh(Context context) {
		getInstance(context).show();
	}

	private FactoryModeMenu(Context context) {
		super(context);
		mContext=context;
	}

	@Override
	public void show() {
		// TODO Auto-generated method stub
		if (init()) {
			super.show();
		} else {
			super.hide();
		}
	}

	private boolean init() {
		// TODO Auto-generated method stub
		boolean bshow = false;
		try {
			
			if (isFactoryMMode(mContext)) {
                                if(!isServiceWork(mContext,"com.homwee.factory.FactoryStateService")){
					Intent intentForState = new Intent(mContext,FactoryStateService.class);
					mContext.startService(intentForState);
                                }
				if (mMTextView == null) {
					mMTextView = new TextView(getContext());
					mMTextView.setText("M");
					mMTextView.setTextColor(android.graphics.Color.RED);
                                        //mMTextView.setAutoSizeTextTypeWithDefaults(TextView.AUTO_SIZE_TEXT_TYPE_UNIFORM);
					//mMTextView.setPadding(screenWidth-120, 1, 1, 0);// left,top,right,bottom
					mMTextView.setPadding(10, 10, 10, 10);// left,top,right,bottom
					mMTextView.setGravity(Gravity.CENTER);
                                        mMTextView.setBackgroundColor(android.graphics.Color.YELLOW);
					if(isSystem4K()) {
						mMTextView.setTextSize(15);
					}
					else{
						mMTextView.setTextSize(20);
					}
					//mMTextView.setBackgroundColor(Color.BLUE);
					addView(mMTextView);


				}
				mMTextView.setVisibility(VISIBLE);
                                Intent intent = new Intent();
				intent.setAction("com.homwee.stateservice.setvisble");
				intent.putExtra("text", "visible:true");
				mContext.sendBroadcast(intent);

				
				bshow = true;
			} else if (mMTextView != null) {
				mMTextView.setVisibility(GONE);
                                Intent intent = new Intent();
				intent.setAction("com.homwee.stateservice.setvisble");
				intent.putExtra("text", "visible:false");
				mContext.sendBroadcast(intent);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		try {
			if (isAgingMode(mContext)) {

				if (mATextView == null) {
					mATextView = new TextView(getContext());
					mATextView.setText("Aging Mode");
					mATextView.setPadding(0, 1, 1, 0);//left, top, right, bottom
					mATextView.setTextSize(30);
					addView(mATextView);
				}
				mATextView.setVisibility(VISIBLE);
				bshow = true;
			} else if (mATextView != null) {
				mATextView.setVisibility(GONE);
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return bshow;
	}

	private boolean isFactoryMMode(Context context) {
		boolean mode = false;
		String str = "0";
		str= MtkTvFApiInformation.getInstance()
				.getIniParameter(Contants.PATH_FACTORYINI,Contants.FACTORY_M_MODE);
		/*try {
			str = Settings.System.getInt(context.getContentResolver(),Contants.FACTORY_M_MODE_GO)==1?"1":"0";
		} catch (SettingNotFoundException e) {
			// TODO Auto-generated catch block
			str = "0";
		}*/
		if(str!=null){
			mode=str.equalsIgnoreCase("1")?true:false;
		}
		else{
			mode=false;
		}
		if(mode){
			Intent intentForBarServer = new Intent(
					"FactoryModeService");
			intentForBarServer.setPackage("com.homwee.factory");
			mContext.startService(intentForBarServer);
		}
		return mode;
	}
	private boolean isAgingMode(Context context) {
		int mode=0;
			mode= MtkTvFApiInformation.getInstance()
					.getIniParameter(Contants.KEYSTATUS_INI_FILEPATH,
							Contants.FACTORY_AGE_MODE).equalsIgnoreCase("1")?1:0;
			/*try {
				mode = Settings.System.getInt(context.getContentResolver(),Contants.FACTORY_AGE_MODE_GO);
			} catch (SettingNotFoundException e) {
				// TODO Auto-generated catch block
				mode=0;
			}*/

		MtkTvFApiDispTstPattern patternMode = new MtkTvFApiDispTstPattern();
		patternMode.mIsVopEnable = mode==1?true:false;
		patternMode.mWindow = 0;
		int ret=MtkTvFApiDisplayTest.getInstance().generateTestPattern(6, patternMode);//
		Log.d("Testpattern",ret+"");

		if(mode==1){
			int backlightInt=1;
			int dreamTimeInt=1;
			int twoHourInt=7200000;
			try{
				backlightInt = Settings.Global.getInt(mContext.getContentResolver(), "picture_backlight");
				//pictureModeInt=Settings.Global.getInt(mContext.getContentResolver(), "picture_mode");
				dreamTimeInt=Settings.System.getInt(mContext.getContentResolver(), SCREEN_OFF_TIMEOUT);
			}catch (Settings.SettingNotFoundException e) {
				e.printStackTrace();
			}
			if(backlightInt!=100||dreamTimeInt!=twoHourInt){
				//Settings.Global.putInt(mContext.getContentResolver(), "picture_mode", 0);
				Settings.Global.putInt(mContext.getContentResolver(), "picture_backlight", 100);
				Settings.System.putInt(mContext.getContentResolver(), SCREEN_OFF_TIMEOUT, twoHourInt);
			}

			MtkTvFApiDisplayTest.getInstance().setBurningMode(true);
			MtkTvUtil.IRRemoteControl(4);

			//for local dimming
			if(factoryPage1.getInstance(mContext).getLDMEnable()==1) {
				Intent imageActivity = new Intent(mContext, TestImagePatternsActivity.class);
				imageActivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				imageActivity.putExtra("agingPattern",1);
				mContext.startActivity(imageActivity);
			}
			Intent intentForBarServer = new Intent(
					"FactoryModeService");
			intentForBarServer.setPackage("com.homwee.factory");
			mContext.startService(intentForBarServer);
		}
		return mode==0?false:true;
	}

	public boolean isSystem4K() {
		String platform = "";
		platform=SystemProperties.get("ro.board.platform");
		//Log.d("Arthur",platform);
		if (platform.equalsIgnoreCase("m7632")||platform.equalsIgnoreCase("m7332")) {
			return true;
		} else if (platform .equalsIgnoreCase("mt5867")) {
			return false;
		} else {
			return false;
		}

	}

        public boolean isServiceWork(Context mContext, String serviceName) {
                if(isServiceWorked == false){
                ActivityManager myAM = (ActivityManager) mContext
                        .getSystemService(Context.ACTIVITY_SERVICE);
                List<ActivityManager.RunningServiceInfo> myList = myAM.getRunningServices(40);
                if (myList.size() <= 0) {
                        return false;
                }
                for (int i = 0; i < myList.size(); i++) {
                        String mName = myList.get(i).service.getClassName().toString();
                        if (mName.equals(serviceName)) {
                                isServiceWorked = true;
                                break;
                        }
                }
                }
                return isServiceWorked;
        }

}
